package com.bafa.objects;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import com.bafa.util.comparators.MatchByDateComparator;
import com.bafa.util.comparators.ScoreByBiggestLossComparator;
import com.bafa.util.comparators.ScoreByBiggestWinComparator;
import com.bafa.util.comparators.ScoreByMostScoredComparator;
import com.bafa.util.comparators.TopScorerComparator;

public class TeamSeason implements Serializable{

	private int numberOfCleanSheets;
	private int numberOfGoalsScored;
	private int numberOfGoalsConceded;
	private int totalGoalsScored;
	private int totalGoalsConceded;

	private int position;
	private int points;
	private int numberOfGamesPlayed;
	private int numberOfGamesWon;
	private int numberOfGamesLost;
	private int numberOfGamesDrawn;
	
	private int totalGamesPlayed;
	private int totalGamesWon;
	private int totalGamesDrawn;
	private int totalGamesLost;
	
	
	private TreeMap<String,Scorer> goalScorers = new TreeMap();
	private LinkedHashMap matches = new LinkedHashMap();
       
	private Score biggestWinScore;
	private Score biggestLossScore;
	private Score mostScored;
	private int division;
	private int seasonId;
	
	
	public TeamSeason(com.bafa.objects.schema.TeamSeason teamSeason){
		this.setDivision(teamSeason.getDivision());
		
		this.setNumberOfCleanSheets(teamSeason.getNumberOfCleanSheets());
		this.setNumberOfGamesDrawn(teamSeason.getNumberOfLeagueGamesDrawn());
		this.setNumberOfGamesLost(teamSeason.getNumberOfLeagueGamesLost());
		this.setNumberOfGamesWon(teamSeason.getNumberOfLeagueGamesWon());
		this.setNumberOfGoalsScored(teamSeason.getNumberOfLeagueGoalsScored());
		this.setNumberOfGoalsConceded(teamSeason.getNumberOfLeagueGoalsConceded());
		this.setNumberOfGamesPlayed(teamSeason.getNumberOfLeagueGamesPlayed());
		
		this.setTotalGamesDrawn(teamSeason.getNumberOfSeasonGamesDrawn());
		this.setTotalGamesLost(teamSeason.getNumberOfSeasonGamesLost());
		this.setTotalGamesWon(teamSeason.getNumberOfSeasonGamesWon());
		this.setTotalGoalsScored(teamSeason.getNumberOfSeasonGoalsScored());
		this.setTotalGoalsConceded(teamSeason.getNumberOfSeasonGoalsConceded());
		this.setTotalGamesPlayed(teamSeason.getNumberOfSeasonGamesPlayed());
		
		this.setPoints(teamSeason.getPoints());
		this.setPosition(teamSeason.getPosition());
		
		this.setSeasonId(teamSeason.getSeasonId());
		
		for(int i=0;i<teamSeason.getMatch().size();i++){
                    if(teamSeason.getMatch().get(i).getID()>0)
			this.addMatch(new Score(teamSeason.getMatch().get(i)));
		}
		
		for(int i=0;i<teamSeason.getScorer().size();i++){
			this.addScorer(new Scorer(teamSeason.getScorer().get(i)));
		}
		
	}
	public int getNumberOfGamesDrawn() {
		
		return numberOfGamesDrawn;
	}
	
	public Score getScore(int i){
		
		return (Score)matches.get(i);
	}
	
	public void setNumberOfGamesDrawn(int gamesDrawn) {
		this.numberOfGamesDrawn = gamesDrawn;
	}
	public int getNumberOfGamesLost() {
		return numberOfGamesLost;
	}
	public void setNumberOfGamesLost(int gamesLost) {
		this.numberOfGamesLost = gamesLost;
	}
	public int getNumberOfGamesPlayed() {
		return numberOfGamesPlayed;
	}
	public void setNumberOfGamesPlayed(int gamesPlayed) {
		this.numberOfGamesPlayed = gamesPlayed;
	}
	public int getNumberOfGamesWon() {
		return numberOfGamesWon;
	}
	public void setNumberOfGamesWon(int gamesWon) {
		this.numberOfGamesWon = gamesWon;
	}
	
	public int getNumberOfCleanSheets() {
		return numberOfCleanSheets;
	}
	public void setNumberOfCleanSheets(int numberOfCleanSheets) {
		this.numberOfCleanSheets = numberOfCleanSheets;
	}
	public int getNumberOfGoalsConceded() {
		return numberOfGoalsConceded;
	}
	public void setNumberOfGoalsConceded(int numberOfGoalsConceded) {
		this.numberOfGoalsConceded = numberOfGoalsConceded;
	}
	public int getNumberOfGoalsScored() {
		return numberOfGoalsScored;
	}
	public void setNumberOfGoalsScored(int numberOfGoalsScored) {
		this.numberOfGoalsScored = numberOfGoalsScored;
	}
	public int getPoints() {
		return points;
	}
	public void setPoints(int points) {
		this.points = points;
	}
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	
	
	/**********ADD METHODS***********/
	public void addMatch(Score score){
		try{
			matches.put(score.getMatchId(),score);
			if(score.getOppoId()>0){
				addBiggestWin(score);
				addBiggestLoss(score);
				addMostScored(score);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	public void addScorer(Scorer scorer){
		if(!scorer.getName().equalsIgnoreCase("o.g.")&&
				!scorer.getName().equalsIgnoreCase("own goal")&&
				!scorer.getName().equalsIgnoreCase("og"))
		goalScorers.put(scorer.getName(),scorer);
	}
	
	
	/********************************/
	public TreeSet getScorers() {
		TreeSet set = new TreeSet(new TopScorerComparator());
		
		set.addAll(goalScorers.values());
		return set;
	}
	public Scorer getScorer(int index) {
		Scorer[] scorer = (Scorer[])getScorers().toArray(new Scorer[getScorers().size()]);
		return scorer[index];
	}
	
	public Score[] getMatches() {
		TreeSet set = new TreeSet(new MatchByDateComparator());
		set.addAll(matches.values());
		return (Score[])set.toArray(new Score[matches.size()]);
	}
	
	public Scorer getTopScorer(){
		return getScorer(0);
	}
	
	public Score getBiggestWin(){
		return biggestWinScore;
	}
	
	public Score getBiggestLoss(){
		return biggestLossScore;
	}
	
	public Score getMostScored(){
		return mostScored;
	}
	
	/***************Object Logic*******************/
	private void addBiggestWin(Score newScore){
		if(biggestWinScore!=null){
			int margin1 = biggestWinScore.getGoalsFor()- biggestWinScore.getGoalsAgainst();
			int margin2 = newScore.getGoalsFor()- newScore.getGoalsAgainst();
			if(margin1==margin2){
				if(biggestWinScore.getGoalsFor()<newScore.getGoalsFor())
					biggestWinScore=newScore;
				else if(biggestWinScore.getDate().after(newScore.getDate()))
					biggestWinScore=newScore;
				
			}else if(margin2>margin1){
				biggestWinScore = newScore;
			}
		}else{
			biggestWinScore = newScore;
		}
	}
	
	private void addBiggestLoss(Score newScore){
		if(biggestLossScore!=null){
			int margin1 = biggestLossScore.getGoalsAgainst()- biggestLossScore.getGoalsFor();
			int margin2 = newScore.getGoalsAgainst()- newScore.getGoalsFor();
		
			if(margin1==margin2){
				if(biggestLossScore.getGoalsAgainst()<newScore.getGoalsAgainst())
					biggestLossScore=newScore;
				else if(biggestLossScore.getDate().after(newScore.getDate()))
					biggestLossScore=newScore;
				
			}else if(margin2>margin1){
				biggestLossScore = newScore;
			}
		}else{
			biggestLossScore = newScore;
		}
	}
	
	private void addMostScored(Score newScore){
		if(mostScored!=null){
			
			if(mostScored.getGoalsFor()<newScore.getGoalsFor()){
				mostScored = newScore;
			}else if(mostScored.getGoalsFor()==newScore.getGoalsFor()){
				if(mostScored.getDate().after(newScore.getDate()))
					mostScored=newScore;
			}
		}else{
			mostScored = newScore;
		}
	}
	/**********************************************/

	public void setBiggestLossScore(Score biggestLossScore) {
		this.biggestLossScore = biggestLossScore;
	}

	public void setBiggestWinScore(Score biggestWinScore) {
		this.biggestWinScore = biggestWinScore;
	}

	public void setDivision(int division) {
		this.division = division;
	}

	

	public void setMostScored(Score mostScored) {
		this.mostScored = mostScored;
	}

	public int getSeasonId() {
		return seasonId;
	}

	public void setSeasonId(int seasonId) {
		this.seasonId = seasonId;
	}
	
	public int getGoalDifference(){
		return numberOfGoalsScored-numberOfGoalsConceded;
	}

	public int getTotalGamesDrawn() {
		return totalGamesDrawn;
	}

	public void setTotalGamesDrawn(int totalGamesDrawn) {
		this.totalGamesDrawn = totalGamesDrawn;
	}

	public int getTotalGamesLost() {
		return totalGamesLost;
	}

	public void setTotalGamesLost(int totalGamesLost) {
		this.totalGamesLost = totalGamesLost;
	}

	public int getTotalGamesPlayed() {
		return totalGamesPlayed;
	}

	public void setTotalGamesPlayed(int totalGamesPlayed) {
		this.totalGamesPlayed = totalGamesPlayed;
	}

	public int getTotalGamesWon() {
		return totalGamesWon;
	}

	public void setTotalGamesWon(int totalGamesWon) {
		this.totalGamesWon = totalGamesWon;
	}

	public int getTotalGoalsConceded() {
		return totalGoalsConceded;
	}

	public void setTotalGoalsConceded(int totalGoalsConceded) {
		this.totalGoalsConceded = totalGoalsConceded;
	}

	public int getTotalGoalsScored() {
		return totalGoalsScored;
	}

	public void setTotalGoalsScored(int totalGoalsScored) {
		this.totalGoalsScored = totalGoalsScored;
	}

	public Score getBiggestLossScore() {
		return biggestLossScore;
	}

	public Score getBiggestWinScore() {
		return biggestWinScore;
	}

	public int getDivision() {
		return division;
	}

	public void setMatches(LinkedHashMap matches) {
		this.matches = matches;
	}

}
